package com.tiyo.system.service.impl;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tiyo.common.utils.StringUtils;
import com.tiyo.system.mapper.SysRoleMapper;
import com.tiyo.system.mapper.domain.SysRole;
import com.tiyo.system.mapper.domain.SysUserRole;
import com.tiyo.system.service.SysRoleService;
import com.tiyo.system.service.SysUserService;
@Service
public class SysRoleServiceImpl implements SysRoleService {
	@Autowired
	SysRoleMapper sysRoleMapper;
	@Autowired
	SysUserService sysUserService;
	@Override
	public Set<String> selectRoleKeys(String userId) {
		List<SysUserRole> userRoles= sysUserService.selectUserRoleByUserId(userId);
		QueryWrapper<SysRole> queryWrapper = new QueryWrapper<SysRole>();
		queryWrapper.select("role_Key");
		queryWrapper.in("id", userRoles.stream().map(userRole->userRole.getRoleId()).collect(Collectors.toList()));
		queryWrapper.eq("status","1");
		List<SysRole> roleList=sysRoleMapper.selectList(queryWrapper);
		Set<String> permsSet = new HashSet<>();
        for (SysRole perm : roleList)
        {
            if (StringUtils.isNotNull(perm))
            {
                permsSet.addAll(Arrays.asList(perm.getRoleKey().trim().split(",")));
            }
        }
        return permsSet;
	}

	@Override
	public List<SysRole> selectRolesByUserId(String userId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<SysRole> selectRoleAll() {
		QueryWrapper<SysRole> queryWrapper = new QueryWrapper<SysRole>();
		return sysRoleMapper.selectList(queryWrapper);
	}

	@Override
	public SysRole selectRoleById(Long roleId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean deleteRoleById(Long roleId) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public int deleteRoleByIds(String ids) throws Exception {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int insertRole(SysRole role) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int updateRole(SysRole role) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int authDataScope(SysRole role) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public String checkRoleNameUnique(SysRole role) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String checkRoleKeyUnique(SysRole role) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void checkRoleAllowed(SysRole role) {
		// TODO Auto-generated method stub

	}

	@Override
	public int countUserRoleByRoleId(Long roleId) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int changeStatus(SysRole role) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int deleteAuthUser(SysUserRole userRole) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int deleteAuthUsers(Long roleId, String userIds) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int insertAuthUsers(Long roleId, String userIds) {
		// TODO Auto-generated method stub
		return 0;
	}

}
